import { EnvironmentType } from '../typing'
import { getLoader } from '../utils'

export const modulePlugin = function (babelType?: EnvironmentType) {
    if (!babelType) {
        return []
    }
    const nodeResolve = getLoader('@rollup/plugin-node-resolve')
    const commonjs = getLoader('@rollup/plugin-commonjs')
    let isNode = false
    if (babelType.includes('node') || !babelType) {
        isNode = true
    }
    const nodeResolveOptions = isNode
        ? { exportConditions: ['import', 'default', 'require', 'node'] }
        : { browser: true, extensions: ['.js', '.ts', '.tsx', '.jsx', '.css', '.scss', '.json'] }
    return [
        nodeResolve(nodeResolveOptions),
        commonjs({
            transformMixedEsModules: true,
            include: [/node_modules/]
        })
    ]
}
